<template>
  <div class="container" v-loading="isLoading">
    <el-card>
      <template #header>
        <div class="table_tool">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="showAddOrEdit = true"
            >新增</el-button
          >
        </div>
      </template>
      <el-table :data="tableData" border>
        <el-table-column prop="img" label="图片">
          <template slot-scope="{ row }">
            <el-image style="width: 200px; height: auto" :src="row.img" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px" align="center">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              @click="openEditRow(row, 'builingForm', initImg)"
              >编辑</el-button
            >
            <el-button type="text" class="red" @click="delRowById(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <footer class="footer">
      <el-pagination
        background
        layout="total, sizes, pager, jumper"
        :current-page="pagination.page"
        :page-sizes="[10, 20, 30, 50, 100]"
        :page-size="pagination.page_size"
        :total="tableTotal"
        @size-change="(val) => (pagination.page_size = val)"
        @current-change="(val) => (pagination.page = val)"
      />
    </footer>

    <el-dialog
      :title="(builingForm.$id ? '编辑' : '新增') + '轮播图'"
      :visible.sync="showAddOrEdit"
      width="888px"
      @closed="() => $refs['builingForm'].resetFields()"
    >
      <el-form
        :model="builingForm"
        :rules="builingRules"
        ref="builingForm"
        label-width="5em"
      >
        <el-form-item prop="$id" hidden />
        <el-form-item prop="$image_uri" hidden/>
        <el-form-item label="房屋id" prop="to">
          <el-select
            v-model="builingForm.to"
            placeholder="请选择所属小区"
          >
            <el-option
              v-for="item in houseList"
              :key="item.id"
              :label="item.id"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <Upload
            v-model="builingForm.$image_uri"
            :file.sync="builingForm.img"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button
          type="primary"
          @click="submitRowData('builingForm')"
          :loading="loading.update"
          >确 定</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<script>
import API from '@/api';
import { table, addOrEdit } from "@/utils/mixins";
import Upload from "@/components/Upload/SingleImage.vue";
export default {
  mixins: [table, addOrEdit],
  components: {
    Upload,
  },
  data: () => ({
    searchForm: {
      $action: ["getTenancyBanner", "deleteTenancyBanner"],
    },
    builingForm: {
      $action: ["addTenancyBanner", "updateTenancyBanner"],
      $id: '',
      $image_uri: "",
      img:'',
      to:"",
    },
    builingRules: {},
    houseList:[],
  }),
  created() {
    this.getHouse();
  },
  methods: {
    initImg({ source, row }) {
      source.$image_uri = row.img;
      delete source.img
    },
    getHouse(){
      API.getTenancyList()
        .then(({ data }) => {
          this.houseList = data;
          console.log(this.houseList)
        })
        .catch(() => {});
    }
  },
};
</script>

<style lang="scss" scoped></style>
